找传奇、传世资源到传世资源站!

android打地鼠小游戏app源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

打地鼠游戏
from clipboard
from clipboard

from clipboard

package com.mingrisoft;import java.util.Random;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity<jm> extends Activity {private int i = 0; // 记录其打到了几只地鼠private ImageView mouse; // 声明一个ImageView对象private Handler handler; // 声明一个Handler对象 private long start; //开始时间 private long end; //结束时间 private boolean flag=true; //声明一个boolean来判断地鼠和地雷private Data data;public int[][] position = new int[][] { // 创建一个表示地鼠位置的数组{ 239, 360 }, { 486, 396 },{ 606, 451 }, { 890,402 }, { 935,323 },{ 630, 325 }, { 600, 275 },{833,257} }; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);data = (Data) getApplication(); //获得自定义的应用程序mouse = (ImageView) findViewById(R.id.imageView1); // 获取ImageView对象mouse.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if(flag) {v.setVisibility(View.INVISIBLE); // 设置地鼠不显示i ;Toast.makeText(MainActivity.this, "打到[ " i " ]只地鼠!",Toast.LENGTH_SHORT).show(); // 显示消息提示框}else {end = System.currentTimeMillis();Toast.makeText(MainActivity.this, "游戏结束!!!",Toast.LENGTH_SHORT).show(); // 显示消息提示框Toast.makeText(MainActivity.this, "用时" (end - start)/1000 "秒" "\n打到[ " i " ]只地鼠!",Toast.LENGTH_SHORT).show(); // 显示消息提示框Intent intent=new Intent(MainActivity.this,Star.class );startActivity(intent);}return false;}});handler = new Handler() {@Overridepublic void handleMessage(Message msg) {int index = 0;if (msg.what == 0x101) {index = msg.arg1; // 获取位置索引值mouse.setX(position[index][0]); // 设置X轴位置mouse.setY(position[index][1]); // 设置Y轴位置mouse.setVisibility(View.VISIBLE); // 设置地鼠显示int ds=new Random().nextInt(10);if(ds<9){flag=true;mouse.setImageResource(R.drawable.mouse);}else {flag=false;mouse.setImageResource(R.drawable.zd);}}super.handleMessage(msg);}};Thread t = new Thread(new Runnable() {@Overridepublic void run() {int index = 0; // 创建一个记录地鼠位置的索引值start = System.currentTimeMillis(); //记录游戏开始时间while (!Thread.currentThread().isInterrupted()) {index = new Random().nextInt(position.length); // 产生一个随机数Message m = handler.obtainMessage(); // 获取一个Messagem.what = 0x101; // 设置消息标识m.arg1 = index; // 保存地鼠标位置的索引值handler.sendMessage(m); // 发送消息try {Thread.sleep(new Random().nextInt(data.getData()) data.getData()); // 休眠一段时间} catch (InterruptedException e) {e.printStackTrace();}}}});t.start(); // 开启线程Button btn2=(Button) findViewById(R.id.button2);btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根end = System.currentTimeMillis();Toast.makeText(MainActivity.this,"用时" (end - start)/1000 "秒" "\n打到[ " i " ]只地鼠!",Toast.LENGTH_SHORT).show(); // 显示消息提示框Intent intent=new Intent(MainActivity.this,Star.class );startActivity(intent);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复